//
//  CJEntranceController.m
//  CJMobileAdDemo
//
//  Created by JJ on 2022/12/1.
//

#import "CJEntranceController.h"
#import "CJEntranceCell.h"
#import "CJEntranceHeaderView.h"

@interface CJEntranceController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *modules;

@end

@implementation CJEntranceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modules = @[
        @{@"key": @"基础广告",
          @"values": @[@"开屏",@"插屏",@"Banner",@"激励视频",@"信息流"],
          @"classNames": @[@"SplashVC", @"InterstitialVC", @"BannerVC", @"RewardVideoVC", @"NativeViewController"]},
      
    ];
    [self setupUI];
}


- (void)setupUI {
    self.title = @"iOS广告对接Demo-倍孜适配器";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

#pragma mark lazy load
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        flowlayout.itemSize = CGSizeMake(( [UIScreen mainScreen].bounds.size.width-8-32)/2, 44);
        flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowlayout.minimumLineSpacing = 16;
        flowlayout.minimumInteritemSpacing = 8;
        flowlayout.headerReferenceSize = CGSizeMake( [UIScreen mainScreen].bounds.size.width, 64);
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowlayout];
        _collectionView.contentInset = UIEdgeInsetsMake(0, 16, 0, 16);
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[CJEntranceHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CJEntranceHeaderView"];
        [_collectionView registerClass:[CJEntranceCell class] forCellWithReuseIdentifier:@"CJEntranceCell"];
    }
    return _collectionView;
}

- (NSArray *)_modules {
    if (!_modules) {
        _modules = [NSArray new];
    }
    return _modules;
}

#pragma mark UICollectionViewDataSource UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([self.modules[section] valueForKey:@"values"]) {
        NSArray *values = (NSArray *)self.modules[section][@"values"];
        return values.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.modules.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CJEntranceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJEntranceCell" forIndexPath:indexPath];
    NSString *contentText = (NSString *)self.modules[indexPath.section][@"values"][indexPath.row];
    [cell setText:contentText];
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader] ) {
        CJEntranceHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CJEntranceHeaderView" forIndexPath:indexPath];
        NSString *headerTitle = (NSString *)self.modules[indexPath.section][@"key"];
        [header setTitle:headerTitle];
        return header;
    }
    return [UICollectionReusableView new];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSString *className = self.modules[0][@"classNames"][indexPath.row];
        [self jumpPage:className];
        return;
    }
}

// 基础广告位
- (void)jumpPage:(NSString *)className {
    Class pageClass = NSClassFromString(className);
    if (pageClass) {
        UIViewController *controller = [pageClass new];
        [self.navigationController pushViewController:controller animated:true];
        return;
    }
}

@end
